package com.metalrain.alm.ui.adapters;

import android.content.Context;
import android.database.DataSetObserver;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.metalrain.alm.AndroidLightManagerApplication;
import com.metalrain.alm.service.HueManager;
import com.metalrain.alm.R;
import com.philips.lighting.model.PHLight;

/**
* Created by administrator on 2014-06-14.
*/
public class MyLightsAdapter extends BaseAdapter{
    private final LayoutInflater layoutInflater;
    public static final String ALL_LIGHTS = "All";

    final static String[] EXTRA_OPTIONS=new String[]{ALL_LIGHTS};


    public MyLightsAdapter(Context ctx) {
        layoutInflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        if (AndroidLightManagerApplication.getLcaService() == null) return 0;
        HueManager hueManager = AndroidLightManagerApplication.getLcaService().mHueManager;
        return hueManager.getGroupAndLightCount()+EXTRA_OPTIONS.length;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        if (AndroidLightManagerApplication.getLcaService().mHueManager.getGroupAndLightCount() == 0) {
            ViewGroup group = (ViewGroup)layoutInflater.inflate(R.layout.fragment_light_list_item,null);
            TextView textView = (TextView) group.findViewById(R.id.textView);
            textView.setText(getItem(i).toString());
            return group;
        }

        ViewGroup group = (ViewGroup)layoutInflater.inflate(R.layout.fragment_light_list_item,null);
        TextView textView = (TextView) group.findViewById(R.id.textView);
        if (getItem(i) instanceof PHLight) {
            PHLight light = (PHLight)getItem(i);
            textView.setText(light.getName());
        } else {
            textView.setText(getItem(i).toString());
        }

        return group;
    }

    @Override
    public boolean areAllItemsEnabled() {
        return true;
    }

    @Override
    public boolean isEnabled(int i) {
        return true;
    }

    @Override
    public void registerDataSetObserver(DataSetObserver dataSetObserver) {


    }

    @Override
    public void unregisterDataSetObserver(DataSetObserver dataSetObserver) {

    }

    @Override
    public Object getItem(int i) {
        try {
            if (i<EXTRA_OPTIONS.length) {
                return EXTRA_OPTIONS[i];
            }
            HueManager hueManager = AndroidLightManagerApplication.getLcaService().mHueManager;
            return hueManager.getGroupOrLight(i-EXTRA_OPTIONS.length);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }


    @Override
    public int getItemViewType(int i) {
        return 0;
    }

    @Override
    public int getViewTypeCount() {
        return 1;
    }

    @Override
    public boolean isEmpty() {
        return false;
    }
}
